package com.zrwl.poscloud.xo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zrwl.poscloud.commons.config.message.WxConfig;
import com.zrwl.poscloud.commons.utils.HttpRequestUtil;
import com.zrwl.poscloud.commons.vo.message.TemplateData;
import com.zrwl.poscloud.xo.service.WxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 微信消息 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2022-11-25
 */
@Service
@Slf4j
public class WxServiceImpl implements WxService {
    @Autowired
    WxConfig wxConfig;
    public static String sendTemplateMessage = "https://api.weixin.qq.com/cgi-bin/message/template/send";//发送模板消息
    @Override
    public String sendTemplateInfo(String openId, String templateId, String url, String first, String remark, String content) throws IOException {
        String backStr = "";
        String accessToken = WxConfig.accessToken;
        if (accessToken == null) {
            accessToken = wxConfig.flushToken();
        }
        boolean repeat = false;
        do {
            //生成要传递的json对象
            Map<String, TemplateData> data = new HashMap<>();
            Map send = new HashMap();
            send.put("touser", openId);
            send.put("template_id", templateId);
            send.put("url", url);
            data.put("first", new TemplateData(first));
            data.put("remark", new TemplateData(remark));
            String[] sonListStr = content.split(",");
            if (sonListStr != null && sonListStr.length > 0) {
                for (int i = 0; i < sonListStr.length; i++) {
                    data.put("keyword" + (i + 1), new TemplateData(sonListStr[i]));
                }
            }
            send.put("data", data);
            String sendStr = JSONObject.toJSONString(send);
            log.info(" sendStr----->" + sendStr);
            Map<String, String> response = HttpRequestUtil.sendPost2(sendTemplateMessage + "?access_token=" + accessToken, sendStr);
            JSONObject jsonData = JSONObject.parseObject(response.get("result"));
            if (!HttpRequestUtil.checkState(response)) {
                throw new RuntimeException("暂时无法连接，可能是由于系统出现故障，请联系管理员人工处理！");
            }
            backStr =JSONObject.toJSONString(jsonData);
            //{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest rid: 61cb0c92-30741540-25d675a7"}
            switch (jsonData.getString("errcode")) {
                case "40001":
                case "42001":
                    accessToken = wxConfig.flushToken();
                    //刷新token再来一遍
                    sendTemplateInfo(openId, templateId, url, first, remark, content);
                    repeat = !repeat;//已经是repeat来的，那就不repeat了
                    break;
                default:
                    break;
            }
        } while (repeat);
        log.info("返回的消息是:" + backStr);
        return backStr;
    }
}
